package com.jzo2o.customer.controller.consumer;


import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "消费者地址薄相关接口")
public class AddressBookController {

    @Resource
    IAddressBookService addressBookService;


    @PostMapping()
    @ApiOperation("新增地址薄")
    public void saveAddressBook(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.saveAddressBook(addressBookUpsertReqDTO);

    }

    @GetMapping("/page")
    @ApiOperation("地址簿分页查询")
    public PageResult<AddressBook> page(AddressBookPageQueryReqDTO addressBookUpsertReqDTO) {
        return addressBookService.getByPage(addressBookUpsertReqDTO);

    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取地址簿信息")
    public AddressBook getAddressBook(@PathVariable long id) {
        return addressBookService.getById(id);
    }


    @PutMapping("/{id}")
    @ApiOperation("根据修改地址簿信息")
    public void updateAddressBook(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.updateAddressById(id,addressBookUpsertReqDTO);
    }


    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除")
    public void deleteAddressBookByIds(@RequestBody List<String> ids) {
        addressBookService.batchDelete(ids);
    }


    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void updateDefaultAddressBook(@RequestParam("flag") Integer flag,@RequestParam("id")Long id) {
        addressBookService.setDefault(flag,id);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBook getDefaultAddressBook() {
        return addressBookService.getDefault();
    }


}
